//
//  WeiBoViewController.m
//  cityStar
//
//  Created by wangjia on 15/3/28.
//  Copyright (c) 2015年 tiny. All rights reserved.
//

#import "WeiBoViewController.h"
#import "WeiBoCell.h"
#import "UIScrollView+SVPullToRefresh.h"
#import "UIScrollView+SVInfiniteScrolling.h"
#import "weiBoList.h"
#import "WeiBoDetailViewController.h"
#import "kata_CommentToolbar.h"

@interface WeiBoViewController ()<CommentToolbarDelegate>{
    NSMutableArray *maryContent;
    kata_CommentToolbar *commentToolbar;
    int intSelect;
}

@property (nonatomic,assign) int page;

@end

@implementation WeiBoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackButton];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData) name:@"weiboRefresh" object:nil];
    intSelect = 0;
    
    self.tableView.sectionHeaderHeight = 0;
    self.tableView.sectionFooterHeight = 0;
    
    self.navigationItem.title = @"城星微博";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"发微博" style:UIBarButtonItemStylePlain target:self action:@selector(sendAction)];
    
    __weak typeof(self)weakSelf = self;
    
    maryContent = [[NSMutableArray alloc]init];
    [self.tableView addPullToRefreshWithActionHandler:^{
        weakSelf.page = 1;
        [weakSelf webRequest_GetVideoMore:NO];
    }];
    [self.tableView addInfiniteScrollingWithActionHandler:^{
        [weakSelf webRequest_GetVideoMore:YES];
    }];
    [self.tableView triggerPullToRefresh];
    
    [self setupToolBar];
}

- (void)refreshData{
    [self.tableView triggerPullToRefresh];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)webRequest_GetVideoMore:(BOOL)isMore
{
    
    NSString *strUrl = [@"" stringByAppendingFormat:@"%@/page/%d",KinterFace_blogPage,isMore?self.page:0];
    
    [HttpRequestManager getMethod:[strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        if (success) {
            DLog(@"%@",response);
            if (isMore) {
                [maryContent addObjectsFromArray:[weiBoList getListArray:response[@"value"][@"content"]]];
                self.page = self.page + 1;
            }else{
                maryContent = [[NSMutableArray alloc]initWithArray:[weiBoList getListArray:response[@"value"][@"content"]]];
            }
            [self.tableView reloadData];
            //            if (maryContent.count == 0) {
            //                self.nothinglabel.hidden = NO;
            //            }else{
            //                self.nothinglabel.hidden = YES;
            //            }
        }
        if (isMore) {
            [self.tableView.infiniteScrollingView stopAnimating];
        }else{
            [self.tableView.pullToRefreshView stopAnimating];
        }
    }];
}

- (void)sendAction{
    [self performSegueWithIdentifier:@"segWeiBoSend" sender:nil];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return maryContent.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 359;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"CellWeiBo";
    WeiBoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    [cell setValueWithValue:maryContent[indexPath.section]];
    [cell.btnHome addTarget:self action:@selector(homeAction) forControlEvents:UIControlEventTouchUpInside];
    [cell.btnNotice addTarget:self action:@selector(leaveMessageAction:) forControlEvents:UIControlEventTouchUpInside];
    [cell.btnNotice2 addTarget:self action:@selector(leaveMessageAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.btnHome.tag = indexPath.section;
    cell.btnNotice.tag = indexPath.section;
    cell.btnNotice2.tag = indexPath.section;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self performSegueWithIdentifier:@"segWeioboDetail" sender:maryContent[indexPath.section]];
}

- (void)homeAction{

}

#pragma mark  评论
-(void)leaveMessageAction:(UIButton *)button{
    intSelect = (int)button.tag;
    if (![commentToolbar superview]) {
        [[UIApplication sharedApplication].keyWindow addSubview:commentToolbar];
        [commentToolbar setFrame:CGRectMake(0, CGRectGetHeight([UIScreen mainScreen].bounds), CGRectGetWidth(self.view.frame), 49)];
    }
    [commentToolbar startEdit];
}

-(void)setupToolBar{
    commentToolbar = [[kata_CommentToolbar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 49)];
    [commentToolbar setCommentToolbarDelegate:self];
}

- (void)postCommentData:(NSString *)data toolbar:(kata_CommentToolbar *)KcommentToolbar
{
    [KcommentToolbar didStopEdit];
    
    weiBoList *weibo = maryContent[intSelect];
    
    NSDictionary *parameters = @{@"type":@"1",
                                 @"id":weibo.id_i,
                                 @"comment":data};
    
    
    [HttpRequestManager postMethod:KinterFace_blogComment parameters:parameters completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        if (success) {
            [SVProgressHUD showSuccessWithStatus:@"评论成功"];
        }else{
            [SVProgressHUD showErrorWithStatus:errMsg];
        }
    }];
    
    
    
}

- (void)reposWithKeyboardHeight:(CGFloat)height animationDuration:(NSTimeInterval)duration curve:(UIViewAnimationCurve)curve
{
    CGRect targetFrame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 49);
    
    targetFrame.origin = (height != 0)?CGPointMake(0, CGRectGetHeight([UIScreen mainScreen].bounds) - height - 49):CGPointMake(0, CGRectGetHeight([UIScreen mainScreen].bounds));
    
    if (height != 0) {
        
        [commentToolbar setHidden:NO];
        
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationCurve:curve];
        [UIView setAnimationDuration:duration];
        [commentToolbar setFrame:targetFrame];
        [UIView commitAnimations];
    }else{
        [commentToolbar setHidden:YES];
        [commentToolbar setFrame:targetFrame];
    }
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"segWeioboDetail"]) {
        WeiBoDetailViewController *viewController = segue.destinationViewController;
        viewController.weiboList = sender;
    }
}
@end
